<template>
    <div>
        <van-tabbar @change="onChange" v-model="active" active-color="#4d1360" inactive-color="#000">
            <van-tabbar-item>
                <span>首页</span>
                <template #icon="props">
                    <img :src="props.active ? icons.home.active : icons.home.inactive">
                </template>
            </van-tabbar-item>
            <van-tabbar-item>
                <span>电影/影院</span>
                <template #icon="props">
                    <img :src="props.active ? icons.film.active : icons.film.inactive">
                </template>
            </van-tabbar-item>
            <van-tabbar-item>
                <span>视频会员</span>
                <template #icon="props">
                    <img :src="props.active ? icons.welfare.active : icons.welfare.inactive">
                </template>
            </van-tabbar-item>
            <van-tabbar-item>
                <span>我的</span>
                <template #icon="props">
                    <img :src="props.active ? icons.user.active : icons.user.inactive">
                </template>
            </van-tabbar-item>
        </van-tabbar>
    </div>
</template>

<script>
export default {
    data() {
        return {
            icons: {
                home: {
                    active: require('../assets/index/home1.png'),
                    inactive: require('../assets/index/home.png')
                },
                film: {
                    active: require('../assets/index/film1.png'),
                    inactive: require('../assets/index/film.png')
                },
                welfare: {
                    active: require('../assets/index/welfare1.png'),
                    inactive: require('../assets/index/welfare.png')
                },
                user: {
                    active: require('../assets/index/source1.png'),
                    inactive: require('../assets/index/source.png')
                }
            },
            active: 0
        }
    },
    created() {
        // 初始化时根据当前路由设置active
        if (this.$route.meta && typeof this.$route.meta.tabIndex !== 'undefined') {
            this.active = this.$route.meta.tabIndex;
        }
    },
    watch: {
        '$route'(to) {
            // 根据路由meta信息更新active状态
            if (to.meta && typeof to.meta.tabIndex !== 'undefined') {
                this.active = to.meta.tabIndex;
            }
        }
    },
    methods: {
        onChange(index) {
            const routes = [
                { name: 'home' },
                { name: 'films' },
                { name: 'welfare' },
                { name: 'user' }
            ];

            if (routes[index]) {
                this.$router.push({ name: routes[index].name });
            }
        }
    }
}
</script>

<style lang="scss" scoped>
::v-deep .van-tabbar {
    width: 375px;
    height: 83px;
    background: #fff;
    border-radius: 0px;
}

/* 默认未选中状态 */
::v-deep .van-tabbar-item__text {
    margin-top: 10px !important;
    color: #B7B7B7;
}

/* 选中状态 */
::v-deep .van-tabbar-item--active .van-tabbar-item__text {
    color: #EE473B !important;
}
</style>